package org.ar.tiendaonline.action;

import java.util.*;

import org.ar.tiendaonline.service.*;
import org.ar.tiendaonline.vo.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class FacturaAction extends ActionSupport{


	private static final long serialVersionUID = 1L;
	
	private ProveedorService provService = new ProveedorDaoService();
	private FacturaService facService = new FacturaDaoService();
	private Factura factura;
	private List<Factura> facturas;
	private List<Proveedor> proveedores;
	
	
	public String getAllFacturas() {
		facturas = facService.getAllFacturas();
		return "success";
	}
	
	public String setUpForInsertOrUpdate(){
		prep();
		if (factura != null && factura.getFacturaId() != 0) {
			factura = facService.getFactura(factura.getFacturaId());
		}
		return "success";
	}
	
	public String insertOrUpdate(){
    	if(!validationSuccessful()){
    		return "input";
    	}else{
    		if (factura.getFacturaId() == null) {
    			facService.insertFactura(factura);
    		} else {
    			facService.updateFactura(factura);
    		}
        	
    	}
    	return "success";
    }
	
	
	public String deleteFactura(){
		facService.deleteFactura(factura.getFacturaId());
		return "success";
	}
	
	private void prep() {
		proveedores = provService.getAllProveedores();
		Map session = ActionContext.getContext().getSession();
		session.put("proveedores", proveedores);
	}
	
	private boolean validationSuccessful(){
    	if(factura.getNroFactura().equals("") || factura.getNroFactura() == null){
    		addActionMessage("Ingresar el numero de factura");
    	}

    	if(factura.getImporte() == null){
    		addActionMessage("Ingresar importe");
    	
    	}
    	if(this.hasActionMessages()){
    		return false;
    	}else{
    		return true;
    	}
    		
    	
    }
	
	public List<Factura> getFacturas() {
		return facturas;
	}

	public void setFacturas(List<Factura> facturas) {
		this.facturas = facturas;
	}
	public Factura getFactura() {
		return factura;
	}
	public void setFactura(Factura factura) {
		this.factura = factura;
	}
	
	

}
